﻿//-----------------------------------------------------------------------
// <copyright file="FileHelper.cs" company="Panchal Softwares">
//     Copyright (c) Panchal Softwares. All rights reserved.
// </copyright>
// <author>Karn Panchal</author>
//-----------------------------------------------------------------------

/// <summary>
/// Namespace Text Finder Library.
/// </summary>
namespace PanchalSoftwares.TextFinder.Library
{
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text.RegularExpressions;

    /// <summary>
    /// Class File Helper.
    /// </summary>
    public class FileHelper
    {
        /// <summary>
        /// Gets or sets a value indicating whether [use case sensitive search].
        /// </summary>
        /// <value>
        /// <c>true</c> if [use case sensitive search]; otherwise, <c>false</c>.
        /// </value>
        public static bool UseCaseSensitiveSearch { get; set; }

        /// <summary>
        /// Determines whether [is all directories accessible] [the specified path].
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>
        /// Returns boolean where all directory inside root directory accessible or not.
        /// </returns>
        public static bool IsAllDirectoriesAccessible(string path)
        {
            string searchPattern = "*.txt";
            DirectoryInfo rootDirectoryInfo = new DirectoryInfo(path);
            try
            {
                List<FileInfo> tempListFileInfo = rootDirectoryInfo.EnumerateFiles(searchPattern, SearchOption.AllDirectories).ToList();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Finds the files.
        /// </summary>
        /// <param name="pathRootDirectory">The path root directory.</param>
        /// <param name="extension">The extension.</param>
        /// <returns>
        /// Returns list of files path.
        /// </returns>
        public static List<FileInfo> FindFiles(string pathRootDirectory, string extension)
        {
            string searchPattern = string.Format("*.{0}", extension);
            List<FileInfo> resultFileList = new List<FileInfo>();
            if (IsAllDirectoriesAccessible(pathRootDirectory))
            {
                DirectoryInfo rootDirectoryInfo = new DirectoryInfo(pathRootDirectory);
                List<FileInfo> rootListFileInfo = rootDirectoryInfo.EnumerateFiles(searchPattern, SearchOption.AllDirectories).ToList();
                foreach (FileInfo itemFileInfo in rootListFileInfo)
                {
                    resultFileList.Add(itemFileInfo);
                }
            }

            return resultFileList;
        }

        /// <summary>
        /// Determines whether [is text available] [the specified file information].
        /// </summary>
        /// <param name="fileInfo">The file information.</param>
        /// <param name="text">The text.</param>
        /// <returns>
        /// Return boolean is text available in file or not.
        /// </returns>
        public static bool IsTextAvailable(FileInfo fileInfo, string text)
        {
            string path = fileInfo.FullName;
            StreamReader reader = new StreamReader(path);
            if (UseCaseSensitiveSearch)
            {
                if (Regex.IsMatch(reader.ReadToEnd(), text, RegexOptions.None))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                if (Regex.IsMatch(reader.ReadToEnd(), text, RegexOptions.IgnoreCase))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// Finds the files.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="extension">The extension.</param>
        /// <param name="text">The text.</param>
        /// <returns>
        /// Returns list of files with text.
        /// </returns>
        public static List<FileInfo> FindFiles(string path, string extension, string text)
        {
            List<FileInfo> resultFiles = new List<FileInfo>();
            List<FileInfo> allFiles = FindFiles(path, extension);
            foreach (FileInfo file in allFiles)
            {
                if (IsTextAvailable(file, text))
                {
                    resultFiles.Add(file);
                }
            }

            return resultFiles;
        }
    }
}
